package org.weed.remote.network.client;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.util.Scanner;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import org.weed.remote.ui.PriceChangeListener;

public class SimpleHttpConnection implements Runnable {
	private URL url;
	private String name;
	private CharsetDecoder decoder;
	private PriceChangeListener priceChangeListener;
	public SimpleHttpConnection(String name, String url, PriceChangeListener priceChangeListener) throws MalformedURLException {
		this.name = name;
		this.url = new URL(url);
		this.priceChangeListener = priceChangeListener;
		decoder = Charset.forName("big5").newDecoder();
	}
	
	public void run() {
		try {
			HttpURLConnection connection = (HttpURLConnection) url.openConnection();
			connection.setRequestMethod("GET");
			
			
			connection.connect();
			
			processContent(new BufferedReader(new InputStreamReader(connection.getInputStream(), decoder)));
			
			connection.disconnect();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void processContent(BufferedReader reader) {
		String content = getContent(reader);	
		Double price = findPrice(content);
		updatePrice(price);		
	}
	
	public String getContent(BufferedReader reader) {
		String line;
		StringBuilder builder = new StringBuilder();		
		try {
			while ((line = reader.readLine()) != null) {
				builder.append(line);
			}
			return builder.toString();			
		} catch (IOException e) {
			return "";
		}
	}
	
	public Double findPrice(String content) {
		Double price = null;
		int index = content.indexOf("▽");
		int factor = 1;
		if (index >= 0) {
			factor = -1;				
		} else {
			index = content.indexOf("△");
		}			
		
		if (index > 0) {
			String value = content.substring(index+1, index+10).trim();
			price = factor * Double.parseDouble(value);
		}
		return price;
	}
	
	public void updatePrice(Double price) {
		if (price != null) {
			if (priceChangeListener != null) {
				priceChangeListener.updatePrice(name, price);
			} else {
				if (price > 0) {
					System.err.println(name + ": " + price);
				} else {
					System.out.println(name + ": " + price);
				}
			}
		}
	}
	
	public static void main(String[] args) throws Exception {
		//System.setProperty("http.proxyHost", "BNESPROXY");
		//System.setProperty("http.proxyPort", "8080");
				
		SimpleHttpConnection c2498 = new SimpleHttpConnection("2498", "http://tw.stock.yahoo.com/q/q?s=2498", null);
		SimpleHttpConnection c3231 = new SimpleHttpConnection("3231", "http://tw.stock.yahoo.com/q/q?s=3231", null);
		SimpleHttpConnection c3008 = new SimpleHttpConnection("3008", "http://tw.stock.yahoo.com/q/q?s=3008", null);
		SimpleHttpConnection c5483 = new SimpleHttpConnection("5483", "http://tw.stock.yahoo.com/q/q?s=5483", null);
		
		ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(2);
		scheduler.scheduleAtFixedRate(c2498, 0, 30, TimeUnit.SECONDS);
//		scheduler.scheduleAtFixedRate(c3231, 0, 30, TimeUnit.SECONDS);
//		scheduler.scheduleAtFixedRate(c3008, 0, 30, TimeUnit.SECONDS);
//		scheduler.scheduleAtFixedRate(c5483, 0, 30, TimeUnit.SECONDS);
		
	}
}
